#include "MCCircleParametricWindow.h"
#include "ui_MCCircleParametricWindow.h"
#include <qvalidator.h>

MCCircleParametricWindow::MCCircleParametricWindow(MCCommand *command, MCCircleVisual *circleVisual, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MCCircleParametricWindow),
    _currentCommand(command),
    _circleVisual(circleVisual),
    _circle(circleVisual->circle())
{
    ui->setupUi(this);

    ui->x1_le->setText(QString::number(_circle.center().x));
    ui->y1_le->setText(QString::number(_circle.center().y));
    ui->radius_le->setText(QString::number(_circle.radius()));
}

MCCircleParametricWindow::~MCCircleParametricWindow()
{
    delete ui;
}

void MCCircleParametricWindow::on_x1_le_textChanged(const QString &arg1)
{
    if (isTextValid(arg1))
    {
        double x = arg1.toDouble();
        _circle = MCCircle2D(MCPoint2D(x, _circle.center().y), _circle.radius());
    }

    updateValue();
}


void MCCircleParametricWindow::on_y1_le_textChanged(const QString &arg1)
{
    if (isTextValid(arg1))
    {
        double y = arg1.toDouble();
        _circle = MCCircle2D(MCPoint2D(_circle.center().x, y), _circle.radius());
    }

    updateValue();
}


void MCCircleParametricWindow::on_radius_le_textChanged(const QString &arg1)
{
    if (isTextValid(arg1))
    {
        double rad = std::abs(arg1.toDouble());
        _circle = MCCircle2D(_circle.center(), rad);
    }

    updateValue();
}


void MCCircleParametricWindow::on_apply_btn_clicked()
{
    this->close();
    _currentCommand->finalize();
}


void MCCircleParametricWindow::on_terminate_btn_clicked()
{
    this->close();
    _currentCommand->terminate();
}

bool MCCircleParametricWindow::isTextValid(QString text)
{
    QDoubleValidator validator;
    int pos = 0;
    QValidator::State state = validator.validate(text, pos);

    if (state == QValidator::Acceptable)
    {
        return true;
    }

    return false;
}

void MCCircleParametricWindow::updateValue()
{
    _circleVisual->update(_circle);
    _currentCommand->presenter_()->redraw();
}

